""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" define the spaceemacs’s style key map
"
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if exists('$vim_simple')
    let g:Lf_fs = '--nowrap --no-auto-preview --fullScreen'
else
    let g:Lf_fs = ''
endif

noremap <space> <nop>
nmap <space>w :w!<cr>
vmap <space>w <esc>:w!<cr>

" TODO - redefine 
nnoremap <space><space> :<c-u>update<CR>

" TODO
noremap <space>[[ :mksession! $vimroot/tmp/vim_session<cr> :wviminfo<cr> 
noremap <space>]] :source $vimroot/tmp/vim_session<cr> :rviminfo<cr> 

" key map of buffer management
noremap <space>bd  :Bclose<cr>:tabclose<cr>gT
noremap <space>ba  :bufdo bd<cr>
noremap <space>bn  :bnext<cr>
noremap <space>bp  :bprevious<cr>
nnoremap <space>bf :bfirst<cr>
noremap <space>bl  :blast<cr>
noremap <space>bb  :cclose<cr>:<c-u><c-r>=printf("Leaderf buffer %s", g:Lf_fs)<cr><cr>
noremap <space>bs  :b#<cr>
noremap <space>bt  :call ToggleMaxWin()<cr>

" key map of tab management
noremap <space>tc :tabnew<cr>
noremap <space>tq :tabclose<cr>
noremap <space>tn :tabnext<cr>
noremap <space>tp :tabprev<cr>
noremap <space>to :tabonly<cr>

" let g:lasttab = 1
" nmap <space>tl :exe "tabn ".g:lasttab<CR>
" au TabLeave * let g:lasttab = tabpagenr()
map <space>te :tabedit <c-r>=expand("%:p:h")<cr>/

" toggle
nnoremap <space>tn :setlocal nonumber! norelativenumber!<cr>
nnoremap <space>tl :setlocal list!<cr>
nnoremap <space>tw :setlocal wrap!<cr>

"tab
noremap <space>1 :tabn 1<cr>
noremap <space>2 :tabn 2<cr>
noremap <space>3 :tabn 3<cr>
noremap <space>4 :tabn 4<cr>
noremap <space>5 :tabn 5<cr>
noremap <space>6 :tabn 6<cr>
noremap <space>7 :tabn 7<cr>
noremap <space>8 :tabn 8<cr>
noremap <space>9 :tabn 9<cr>
noremap <space>0 :tabn 10<cr>

" windows
nnoremap <space>ww <C-W>w
nnoremap <space>wr <C-W>r
nnoremap <space>wd <C-W>c
nnoremap <space>wq <C-W>q
nnoremap <space>wj <C-W>j
nnoremap <space>wk <C-W>k
nnoremap <space>wh <C-W>h
nnoremap <space>wl <C-W>l
tnoremap <space>wj <C-W>j
tnoremap <space>wk <C-W>k
tnoremap <space>wh <C-W>h
tnoremap <space>wl <C-W>l
nnoremap <space>wH <C-W>5<
nnoremap <space>wL <C-W>5>
nnoremap <space>wJ :resize +5<CR>
nnoremap <space>wK :resize -5<CR>
nnoremap <space>w= <C-W>=
nnoremap <space>ws <C-W>s
nnoremap <space>w- <C-W>s
nnoremap <space>wv <C-W>v
nnoremap <space>w\| <C-W>v
nnoremap <space>w2 <C-W>v

tnoremap <silent><esc>     <C-\><C-n>

" fold
nnoremap <space>f0 :set foldlevel=0<CR>
nnoremap <space>f1 :set foldlevel=1<CR>
nnoremap <space>f2 :set foldlevel=2<CR>
nnoremap <space>f3 :set foldlevel=3<CR>
nnoremap <space>f4 :set foldlevel=4<CR>
nnoremap <space>f5 :set foldlevel=5<CR>
nnoremap <space>f6 :set foldlevel=6<CR>
nnoremap <space>f7 :set foldlevel=7<CR>
nnoremap <space>f8 :set foldlevel=8<CR>
nnoremap <space>f9 :set foldlevel=9<CR>

" replace
map <space>r :%s/\<<c-r><c-w>\>/<c-r><c-w>/gg

" search
noremap <space>ss :<c-u><c-r>=printf("Leaderf rg -e %s -s -w %s", expand("<cword>"), "")<cr><cr>
noremap <space>sf <esc>:cclose<cr>::<c-u><c-r>=printf("Leaderf rg --regexMode %s", g:Lf_fs)<cr><cr>
nnoremap <silent> <space>sa <ESC>:cclose<cr>:Ack -sw <c-r><c-w><cr>
nnoremap <space>se <ESC>:cclose<cr>:Leaderf rg -e<space> 
noremap <space>so :<C-U><C-R>=printf("Leaderf rg --recall %s", g:Lf_fs)<cr><cr>
noremap <space>sl :<C-U><C-R>=printf("Leaderf line %s --regexMode --nowrap", "")<cr><cr>
noremap <space>st :<C-U><C-R>=printf("Leaderf bufTag %s", g:Lf_fs)<cr><cr>
noremap <space>nn <esc>:cclose<cr>:Leaderf --next<cr>
noremap <space>pp <esc>:cclose<cr>:Leaderf --previous<cr>

" map <space>sa zg
map <space>s? z=

" file
nnoremap <silent> <space>ff :<c-u><c-r>=printf("Leaderf file --nameOnly %s", g:Lf_fs)<cr><cr>
nnoremap <space>fs :update<cr>
nnoremap <space>fa :wa<cr>
nnoremap <silent> <space>fr <esc>:cclose<cr>:<c-u><c-r>=printf("Leaderf mru %s", g:Lf_fs)<cr><cr>
nnoremap <silent> <space>fm <esc>:cclose<cr>:<c-u><c-r>=printf("Leaderf mru --cwd %s", g:Lf_fs)<cr><cr>

let g:Lf_ShortcutB = '<space>bb'
let g:Lf_ShortcutF = '<space>ff'

" language
noremap <space>lf <esc>:cclose:<c-u><c-r>=printf("Leaderf function %s", g:Lf_fs)<cr><cr>
noremap <space>lt <esc>:cclose<cr>:Clap tags<cr>
noremap <c-f> <esc>:cclose:<c-u><c-r>=printf("Leaderf! rg -e %s", g:Lf_fs)<cr>
nnoremap <silent> <c-f> <esc>:cclose<cr>:Leaderf rg --regexMode<cr>
noremap <space>ll <esc>:cclose<cr>:CocFzfList outline<cr>

"reselect the edited text
" nnoremap <leader>v V`]

" Split fast
nnoremap <space>\ :vs<CR>
nnoremap <space>- :sp<CR>

" Remove the ^M
" noremap <space>m mmHmt:%s/<C-V><cr>//ge<cr>'tzt'm

noremap <silent><space>tq :call Toggle_QuickFix(6)<cr>

" TODO
" map <space>n :cn<cr>
" map <space>p :cp<cr>
" map <space>e :e! $vimroot/user/vimrc/local.vim<cr>

" TODO  not effect
autocmd! BufWritePost $vimroot/user/vimrc/local.vim source $vimroot/user/vimrc/local.vim

" C++ comment to C 
noremap <space>CC :s://\s*\(.*\):/* \1 */:<CR>
" C comment to C++
noremap <space>CD :%s:/\*\(.\{-\}\)\s*\*/://\1:<CR>

" note: replace cc
map <space>cc :botright cope<cr>
map <space>co :ccl<cr>:cope<cr>ggVGy:tabnew<cr>:set syntax=qf<cr>pgg

map <space>cc :AsyncRun make<cr>

nmap <space>a= :Tabularize /=<CR>
vmap <space>a= :Tabularize /=<CR>
nmap <space>a: :Tabularize /:\zs<CR>
vmap <space>a: :Tabularize /:\zs<CR>

nmap <space>jj :Translate<CR>
vmap <space>jj :Translate<CR>

" for fugitive
nnoremap <space>dsj :Git log --pretty=format:"%h\|\|%as-[%an]: %s" -- %<cr>
nnoremap <space>dsk 0"ayiw:bw<cr>:rightbelow Gvdiff <c-r>a<cr>

" for coc diag
nnoremap <space>dd <esc>:cclose<cr>:CocFzfList diagnostics<cr>
nnoremap <space>dn :call CocAction('diagnosticNext')<CR>
nnoremap <space>dp :call CocAction('diagnosticPrevious')<CR>
" fold file
nnoremap <space>fd :call CocAction('fold')<CR>

" TODO
nnoremap <space>m :set invpaste<CR>
